package com.lance.modules.third.alipay;



import java.text.SimpleDateFormat;
import java.util.Date;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayTradeAppPayModel;
import com.alipay.api.domain.AlipayTradeWapPayModel;
import com.alipay.api.request.AlipayTradeAppPayRequest;
import com.alipay.api.request.AlipayTradeWapPayRequest;
import com.lance.core.base.BaseResful;
import com.lance.core.base.VResfulMapping;

import io.vertx.core.eventbus.Message;

@VResfulMapping
public class AlipayResful extends BaseResful{
	
	@VResfulMapping(path="app/pay")
	public void appPay(String params,Message<Object> message){
		vertx.executeBlocking(bc->{
				// 商户订单号，商户网站订单系统中唯一订单号，必填
			    String out_trade_no = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
				// 订单名称，必填
			    String subject = "手机网站支付测试商品";
			    // 付款金额，必填
			    String total_amount="0.01";
			    // 商品描述，可空
			    String body ="购买测试商品0.01元";
			    // 超时时间 可空
			   String timeout_express="2m";
			    // 销售产品码 必填
			    String product_code="QUICK_WAP_WAY";
			    
			    
			    /**********************/
			    // SDK 公共请求类，包含公共请求参数，以及封装了签名与验签，开发者无需关注签名与验签     
			    //调用RSA签名方式
			    AlipayClient client = new DefaultAlipayClient(AlipayConfig.URL, AlipayConfig.APPID, AlipayConfig.RSA_PRIVATE_KEY, AlipayConfig.FORMAT, AlipayConfig.CHARSET, AlipayConfig.ALIPAY_PUBLIC_KEY,AlipayConfig.SIGNTYPE);
			    AlipayTradeAppPayRequest alipay_request=new AlipayTradeAppPayRequest();
			    
			    // 封装请求支付信息
			    AlipayTradeAppPayModel model=new AlipayTradeAppPayModel();
			    model.setOutTradeNo(out_trade_no);
			    model.setSubject(subject);
			    model.setTotalAmount(total_amount);
			    model.setBody(body);
			    model.setTimeoutExpress(timeout_express);
			    model.setProductCode(product_code);
			    alipay_request.setBizModel(model);
			    // 设置异步通知地址
			    alipay_request.setNotifyUrl(AlipayConfig.notify_url);
			    // 设置同步地址
			    alipay_request.setReturnUrl(AlipayConfig.return_url);   
			    
			    // form表单生产
			    String form = "";
				try {
					// 调用SDK生成表单
					form = client.sdkExecute(alipay_request).getBody();
					bc.complete(form);
				} catch (AlipayApiException e) {
					e.printStackTrace();
				} 
			
		}, res->{
			message.reply(res.result().toString());
		});
	}
	
	@VResfulMapping(path="wap/pay")
	public void wapPay(String params,Message<Object> message){
		vertx.executeBlocking(bc->{
				// 商户订单号，商户网站订单系统中唯一订单号，必填
			    String out_trade_no = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
				// 订单名称，必填
			    String subject = "手机网站支付测试商品";
			    // 付款金额，必填
			    String total_amount="0.01";
			    // 商品描述，可空
			    String body ="购买测试商品0.01元";
			    // 超时时间 可空
			   String timeout_express="2m";
			    // 销售产品码 必填
			    String product_code="QUICK_WAP_WAY";
			    
			    
			    /**********************/
			    // SDK 公共请求类，包含公共请求参数，以及封装了签名与验签，开发者无需关注签名与验签     
			    //调用RSA签名方式
			    AlipayClient client = new DefaultAlipayClient(AlipayConfig.URL, AlipayConfig.APPID, AlipayConfig.RSA_PRIVATE_KEY, AlipayConfig.FORMAT, AlipayConfig.CHARSET, AlipayConfig.ALIPAY_PUBLIC_KEY,AlipayConfig.SIGNTYPE);
			    AlipayTradeWapPayRequest alipay_request=new AlipayTradeWapPayRequest();
			    
			    // 封装请求支付信息
			    AlipayTradeWapPayModel model=new AlipayTradeWapPayModel();
			    model.setOutTradeNo(out_trade_no);
			    model.setSubject(subject);
			    model.setTotalAmount(total_amount);
			    model.setBody(body);
			    model.setTimeoutExpress(timeout_express);
			    model.setProductCode(product_code);
			    alipay_request.setBizModel(model);
			    // 设置异步通知地址
			    alipay_request.setNotifyUrl(AlipayConfig.notify_url);
			    // 设置同步地址
			    alipay_request.setReturnUrl(AlipayConfig.return_url);   
			    
			    // form表单生产
			    String form = "";
				try {
					// 调用SDK生成表单
					form = client.pageExecute(alipay_request).getBody();
					bc.complete(form);
				} catch (AlipayApiException e) {
					e.printStackTrace();
				} 
			
		}, res->{
			message.reply(res.result().toString());
		});
	}
	
	
}
